package calao;

import java.util.HashMap;

import calao.client.MemberService;
import calao.client.MemberInfo;
import calao.client.MissingPermissionException;
import calao.client.ProfileInfo;

import static calao.util.Logger.*;

public class MemberServiceImpl 
extends DesktopServiceImpl 
implements MemberService 
{

	public MemberInfo detailMember(long guid) 
	throws MissingPermissionException 
	{
		check(ADMIN_MEMBERS);
		Membership mship = ws.getMembershipManager().getMembership(guid);
		User user = ws.getUserManager().getUser(guid);
		if (mship==null || user==null) return null;
		return toMemberInfo(user, mship, 1);
	}

	public MemberInfo[] listMembers() 
	{
		Membership[] mships = ws.getMembershipManager().listMembers();
		MemberInfo[] result = new MemberInfo[mships.length];
		int count = 0;
		for (int i=0; i<result.length; i++) {
			User user = ws.getUserManager().getUser(mships[i].guid);
			if (user!=null) {
				result[count++] = toMemberInfo(user, mships[i], 0);
			}
			else {
				log(ERROR, "Cannot find user %d", mships[i].guid);
			}
		}
		return java.util.Arrays.copyOf(result, count);
	}

	public void modify(MemberInfo info) 
	throws MissingPermissionException 
	{
		check(ADMIN_MEMBERS);
		User user = ws.getUserManager().getUser(info.getGlobalUserId());
		Membership mship = ws.getMembershipManager().getMembership(info.getGlobalUserId());
		if (user==null || mship==null) throw new IllegalArgumentException();
		if (info.getPassword()!=null) user.setPassword(info.getPassword());
		if (info.getAlias()!=null) {
			user.setAlias(info.getAlias());
			user.setEmail(info.getEmail());
		}
		if (info.getName()!=null) {
			mship.setName(info.getName());
			mship.setInactive(info.isInactive());
			mship.setProfiles(info.getProfiles());
		}
		user.save();
		mship.save();
	}
	
	public ProfileInfo[] listProfiles()
	throws MissingPermissionException 
	{
		check(ADMIN_MEMBERS);
		Profile[] profiles = ws.getProfileManager().listProfiles();
		ProfileInfo[] result = new ProfileInfo[profiles.length];
		for (int i=0; i<result.length; i++) {
			result[i] = toInfo(profiles[i]);
		}
		return result;
	}

	public MemberInfo create(MemberInfo info) 
	throws MissingPermissionException
	{
		check(ADMIN_MEMBERS);
		User newUser = ws.createMemberUser(info.getAlias(), info.getPassword(), 
				info.getName(),	info.getProfiles(), getProperties(info));
		return toMemberInfo(newUser, 
				ws.getMembershipManager().getMembership(newUser.getGlobalUserId()), 1);
	}
			
	MemberInfo toMemberInfo(User user, Membership mship, int detailLevel)
	{
		MemberInfo info = new MemberInfo(user.getGlobalUserId());
		info.setName(mship.name);
		info.setProfiles(mship.profiles);
		info.setInactive(mship.isInactive());
		if (detailLevel>0) {
			info.setAlias(user.alias);
			info.setEmail(user.email);
		}
		return info;
	}
	
	@SuppressWarnings("unchecked")
	static HashMap<String,String> getProperties(MemberInfo info)
	{
		return (HashMap<String,String>)info.getProperties();
	}
	
	ProfileInfo toInfo(Profile profile)
	{
		ProfileInfo info = new ProfileInfo(profile.id, profile.name);
		info.setPermissionNames(profile.getPermissions());
		return info;
	}

	public void delete(ProfileInfo profile) 
	throws MissingPermissionException 
	{
		check(ADMIN_MEMBERS);		
		ws.getProfileManager().deleteProfile(profile.getId());
	}

	public void save(ProfileInfo profile) 
	throws MissingPermissionException 
	{
		check(ADMIN_MEMBERS);		
		ws.getProfileManager().storeProfile(profile.getId(), profile.getName(),
				profile.getPermissionNames());
	}
	
}
